<!-- 组织结构 -->
<template>
  <div class="module_">
    <div class="mb8">
      <search>
        <el-form label-width='80px' inline autocomplete="off"  :model="pageparm" class="search_box">
          <el-form-item label="科室名称">
            <el-input clearable
              size="medium"
              v-model="pageparm.name"
              placeholder="请输入科室名搜索"
            ></el-input>
          </el-form-item>
          <el-form-item label="科室号">
            <el-input clearable
              size="medium"
              v-model="pageparm.code"
              placeholder="请输入科室号搜索"
            ></el-input>
          </el-form-item>
          <el-form-item label="科室类型">
            <el-select v-model="pageparm.type" clearable @change='searchChane($event,"type")' placeholder="请选择科室类型">
              <el-option
                v-for="item in keshiType"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <template slot="footer" class="flex">
          <el-button class="mr8" size="medium" type="primary" @click="handle(null,{},3)">新增科室</el-button>
          <exportExcel :apiFn='Apidepartments' name='科室管理'  />
        </template>
      </search>
    </div>
    <el-table
      :data="tableData"
      class="flex-1"
      height="100%" size="medium" 
      border 
      highlight-current-row
      v-loading="tableLoading"
      stripe
      @sort-change="tableSortChange">  
      <el-table-column
        align="center"
        v-for="(item,i) in headerList"
        :key="item.prop"
        :prop="item.prop"
        :label="item.label"
        :min-width="item.width"
        :fixed="i === 0 ? true: false"
        show-overflow-tooltip
        :sortable="item.noSortable ? false:'custom'">
        <template slot-scope="scope">
          <template v-if="item.prop === 'type'">
            {{keshiTypeObj[scope.row.type]}}
          </template>
          <template v-else>
            {{scope.row[item.prop]}}
          </template>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-tooltip  effect="dark" content="修改信息" placement="bottom-end">
            <el-button size="small"  @click="handle(scope.$index, scope.row,2)" icon="el-icon-edit" circle></el-button>
          </el-tooltip>
          <el-tooltip  effect="dark" content="删除" placement="bottom-end">
            <el-button size="small" type="danger" @click="deleteUser(scope.$index, scope.row)" icon="el-icon-delete" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>

    <el-dialog :visible.sync="editFormVisible" width="400px" @click="editFormVisible = false"
    :close-on-click-modal='false' :close-on-press-escape='false' @close='dialogClose'>
      <h2 slot="title" class="tx-c">{{dialogTitle}}</h2>
      <el-form autocomplete="off"  label-width="100px" :model="editForm" class="form_flex">
        <el-form-item label="科室名" class="tip">
          <el-input
            size="medium"
            type="text"
            v-model="editForm.name"
            clearable
            placeholder="请输入科室名称"/>
        </el-form-item>
        <el-form-item label="科室号" class="tip">
          <el-input
            size="medium"
            type="number"
            v-model="editForm.code"
            clearable
            placeholder="请输入科室号"/>
        </el-form-item>
        <el-form-item label="科室类型" class="tip">
          <el-select v-model="editForm.type" placeholder="请选择科室类型">
            <el-option
              v-for="item in keshiType"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科">
          <inputSearch ref="search_x" type="xueke" :middleList="middleList" @onChange="onChange($event,'discipline_id')" />
        </el-form-item>
      </el-form> 
      <div slot="footer" class="flex-justify-bcenter">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit" :loading='btnLoading'>确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {
  Apidepartments,ApidepartmentsAdd,ApidepartmentsPut,ApidepartmentsDel
} from '@/api'
import { debounce } from "@/utils/index";
import {headerList} from '../js/structure'
import {mapActions} from 'vuex'
import {keshiType,keshiTypeObj} from '@/assets/js'
export default {
  name:'structure',
  computed:{},
  data(){
    return{
      keshiType,
      keshiTypeObj,
      Apidepartments,
      tableData:[], 
      editFormVisible: false, 
      editForm: {},  //修改或者查看详情

      pageparm: {
        page: 1,
        perPage: 20,
        total: null,
        sort_str: null, 
        sort_order: 'desc' ,
        name: null,
        code: null, 
      },
      tableLoading: false,
      btnLoading: false,

      headerList,
      rules:['name','code','type'],
      dialogTitle:'',
      middleList:[],
    }
  },
  mounted(){
    this.getList()
  },
  methods:{
    dialogClose(){
      this.$refs.search_x.clear()
    },
    onChange(id,ele){
      this.editForm[ele] = id
    },
    searchChane(i,val){
      this.pageparm[val] = i
      this.pageparm.page = 1
      this.getList()
    },
    ...mapActions(["getCacheDepartments"]),

    tableSortChange(column){
      if (column.order === "descending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "desc";
      } else if (column.order === "ascending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "asc";
      }else{
        this.pageparm.sort_str = null;
        this.pageparm.sort_order = null;
      }
      this.getList()
    },
    //搜索
    // clearSearch(){
    //   this.$refs.d_Search.clear()
    // },
    // d_Change(id){
    //   this.pageparm.id = id
    //   this.pageparm.page = 1
    //   this.getList()
    // },
    //分页
    callFather(pageparm){
      this.pageparm.page = pageparm.page
      this.pageparm.perPage = pageparm.perPage
      this.getList()
    },
    getList(){
      this.tableLoading = true
      Apidepartments(this.pageparm).then(({data,page}) => {
        this.tableData = data || []
        this.pageparm.total = page.total
        this.tableLoading = false
      }).catch(()=>{
        this.tableLoading = false
      })
    },
    handle(index,value,i){
      this.editForm = {...value}
      this.editFormVisible = true
      this.dialogTitle = i === 3 ? '新增科室' : '修改科室信息'
      // this.$nextTick(()=>{
        if(value.discipline_id) this.middleList=[{id:value.discipline_id,name:value.disciplines_name}]
      // })
    },
    departmentsChange(id){
      this.editForm.belong_department_id = id
    },

    deleteUser(index,value){
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({
            lock: true,
            text: '加载中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          ApidepartmentsDel(value.id).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList()
            this.$nextTick(()=>{
              this.getCacheDepartments()
            })
          }).finally(()=>{
            loading.close()
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    submit : debounce(function(){   //提交  修改或新增
      const options = {...this.editForm}
      for (const key of this.rules) {
        if(!options[key] && options[key] !== 0){
          this.$message({
            type: 'error',
            message: '必填信息请填写完整！'
          });
          return
        }
      }
      this.btnLoading = true
      if(options.id){   //有ID就是修改
        ApidepartmentsPut(options,options.id).then(res => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          this.editFormVisible = false
          this.getList()
          this.$nextTick(()=>{
            this.getCacheDepartments()
          })
        }).catch(()=>{
          this.$message({
            type: 'error',
            message: '修改失败!'
          });
        }).finally(()=>{
            this.btnLoading = false
        });
      }else{
        ApidepartmentsAdd(options).then(res => {
          this.$message({
            type: 'success',
            message: '添加成功!'
          });
          this.editFormVisible = false
          this.getList()
          this.$nextTick(()=>{
            this.getCacheDepartments()
          })
        }).catch(()=>{
          this.$message({
            type: 'error',
            message: '添加失败!'
          });
        }).finally(()=>{
            this.btnLoading = false
        });
      }
    },800,true)
  },

}
</script>

<style lang='less' scoped>
</style>